/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cliente.java
 *
 * Created on 28/07/2011, 03:47:22 AM
 */
package dced.ui;
import dced.entity.Cliente;
import dced.util.HibernateUtil;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import org.hibernate.*;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import javax.swing.JOptionPane;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.JFileChooser;
/**
 *
 * @author swicho
 */
public class cliente extends javax.swing.JFrame {

    /** Creates new form cliente */
    public cliente() {
        initComponents();
         runQuery(QUERY_ALL);
    }
    
    
    private void actualizarActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        //Inventario inv=new Inventario();
        runQuery(QUERY_ALL);
    }
    
    private static String QUERY_ALL="from Cliente";
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idTF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        nombreTF = new javax.swing.JTextField();
        direccionTF = new javax.swing.JTextField();
        apemTF = new javax.swing.JTextField();
        apepTF = new javax.swing.JTextField();
        rfcTF = new javax.swing.JTextField();
        telefonoTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        correoTF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jToggleButton2 = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("ID");

        jButton1.setText("Alta");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "IID", "Nombre", "Ap. Paterno", "Ap. Materno", "Direccion", "RFC", "Telefono", "Email"
            }
        ));
        resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(resultTable);

        direccionTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                direccionTFActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre");

        jLabel3.setText("Apelldo Paterno");

        jLabel4.setText("Apellido Materno");

        jLabel5.setText("Direccion");

        jLabel6.setText("RFC");

        jLabel7.setText("Telefono");

        jLabel8.setText("Correo");

        jButton3.setText("Buscar por Apellido");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Modificar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Baja");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel9.setText("Ap. Paterno");

        jLabel10.setFont(new java.awt.Font("Ubuntu", 1, 24));
        jLabel10.setText("Gestion de Clientes");

        jToggleButton1.setText("Buscar por ID");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jToggleButton2.setText("Generar reporte");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                    .addComponent(apepTF)
                    .addComponent(apemTF)
                    .addComponent(direccionTF))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rfcTF, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                    .addComponent(telefonoTF, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                    .addComponent(correoTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                .addGap(381, 381, 381))
            .addGroup(layout.createSequentialGroup()
                .addGap(325, 325, 325)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(368, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(43, 43, 43)
                                .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(27, 27, 27)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                .addGap(48, 48, 48)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButton2)
                .addGap(145, 145, 145))
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(117, 117, 117))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(rfcTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apepTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(telefonoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apemTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(correoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(direccionTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(99, 99, 99))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jButton3)
                                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel9)
                                            .addComponent(jButton4))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jToggleButton1)
                                            .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel1)
                                            .addComponent(jButton5)))
                                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                                    .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                                .addGap(21, 21, 21))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jToggleButton2)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private void runQuery(String hql) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(hql);
            java.util.List resultList = q.list();
            muestraResultado(resultList);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
    }
    
   /* private void runQuery() {
        executeHQLQuery(QUERY_BASED_ON_ID + idTF.getText());
    }*/
    private void busqueda(String str){
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        
    }
    
    private void agregaCliente(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Cliente cliente = new Cliente();
        cliente.setNombreCliente(nombreTF.getText());
        cliente.setApellidoPaternoCliente(apepTF.getText());
        cliente.setApellidoMaternoCliente(apemTF.getText());
        cliente.setDireccionCliente(direccionTF.getText());
        cliente.setRfcCliente(rfcTF.getText());
        cliente.setTelefonoCliente(telefonoTF.getText());
        cliente.setCorreoCliente(correoTF.getText());
        session.save(cliente);
        session.flush();
        session.clear();
        session.close();
    }
    
    
    private void muestraResultado(java.util.List resultList) {
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("Id"); 
    tableHeaders.add("Nombre");
    tableHeaders.add("Ap Paterno");
    tableHeaders.add("Ap. Materno");
    tableHeaders.add("Direccion");
    tableHeaders.add("RFC");
    tableHeaders.add("Telefono");
    tableHeaders.add("Email");

    for(Object o : resultList) {
        Cliente cliente = (Cliente)o;
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(cliente.getIdCliente());
        oneRow.add(cliente.getNombreCliente());
        oneRow.add(cliente.getApellidoPaternoCliente());
        oneRow.add(cliente.getApellidoMaternoCliente());
        oneRow.add(cliente.getDireccionCliente());
        oneRow.add(cliente.getRfcCliente());
        oneRow.add(cliente.getTelefonoCliente());
        oneRow.add(cliente.getCorreoCliente());
        tableData.add(oneRow);
    }
    resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
}
    
    private void modificaCliente(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        //Cliente cliente = (Cliente) session.load(Cliente.class, idTF.getText());
        Cliente cliente = new Cliente();
        cliente.setIdCliente(Integer.parseInt(idTF.getText()));
        cliente.setNombreCliente(nombreTF.getText());
        cliente.setApellidoPaternoCliente(apepTF.getText());
        cliente.setApellidoMaternoCliente(apemTF.getText());
        cliente.setDireccionCliente(direccionTF.getText());
        cliente.setRfcCliente(rfcTF.getText());
        cliente.setTelefonoCliente(telefonoTF.getText());
        cliente.setCorreoCliente(correoTF.getText());
        session.update(cliente);
        session.flush();
        session.clear();
        session.close();
    } 
    
    
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        agregaCliente();
        runQuery(QUERY_ALL);
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

             Session session = HibernateUtil.getSessionFactory().openSession();
             session.beginTransaction();       
             Query q = session.createQuery("from Cliente where Apellido_Paterno_Cliente like :APpater");
             q.setParameter("APpater", "%" + jTextField1.getText() + "%");     
             muestraResultado(q.list());
             session.getTransaction().commit();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        modificaCliente();
        cliente.this.actualizarActionPerformed(evt);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Cliente cliente = new Cliente();
        cliente.setNombreCliente("hola");
        cliente.setApellidoPaternoCliente("hola");
        cliente.setApellidoMaternoCliente("hola");
        cliente.setDireccionCliente("hola");
        cliente.setRfcCliente("hola");
        cliente.setTelefonoCliente("hola");
        cliente.setCorreoCliente("hola");
        cliente.setIdCliente(Integer.parseInt(idTF.getText()));
        session.delete(cliente);
        session.flush();
        session.clear();
        session.close();
        runQuery(QUERY_ALL);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void direccionTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_direccionTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_direccionTFActionPerformed

    private void resultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseClicked
        
        idTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),0)));
        nombreTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),1)));
        apepTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),2)));
        apemTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),3)));
        direccionTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),4)));
        rfcTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),5)));
        telefonoTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),6)));
        if(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),7)) == null)
        {
        correoTF.setText(" ");
        }
        else
        {
        correoTF.setText(String.valueOf(resultTable.getValueAt(resultTable.getSelectedRow(),7)));
        }
        
    }//GEN-LAST:event_resultTableMouseClicked

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    
    }//GEN-LAST:event_jTextField1ActionPerformed
 
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        Session session = HibernateUtil.getSessionFactory().openSession();
             session.beginTransaction();       
             Query q = session.createQuery("from Cliente where ID_Cliente like :id");
             q.setParameter("id", idTF.getText());   
             muestraResultado(q.list());
             session.getTransaction().commit();
            
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
       
        
        JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf", "PDF");
        fc.setFileFilter(filter);
        int returnVal = fc.showSaveDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                java.io.File file = fc.getSelectedFile();
                //This is where a real application would save the file.

                String ruta = file.getPath();
                ruta = (ruta.toLowerCase().endsWith(".pdf")) ? ruta : (ruta + ".pdf");
                
        Document document = new Document(PageSize.A4);
        try{
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(ruta));
            writer.setPdfVersion(PdfWriter.VERSION_1_6);
            document.addTitle("Reporte_clientes");
             document.open();
             Image foto = Image.getInstance("/home/kreuz_katze/Imágenes/DCED_logo.png");
                        foto.scaleToFit(500, 500);
                        foto.setAlignment(Chunk.ALIGN_CENTER);
                        document.add(foto);
           /* foto = Image.getInstance("/home/kreuz_katze/Imágenes/DCED_datos.png");
            foto.scaleToFit(300, 200);
            foto.setAlignment(Chunk.ALIGN_RIGHT);
            document.add(foto);*/
             Paragraph par = new Paragraph("Reporte de clientes",FontFactory.getFont("arial", 14, BaseColor.BLACK));
            par.setIndentationLeft(par.ALIGN_JUSTIFIED_ALL);
            par.setAlignment(par.TITLE);
            par.getExtraParagraphSpace();
            
            int columnas = resultTable.getColumnCount();
            int filas = resultTable.getRowCount();
            
            String numCadena[]=new String[300];
                    int y=0;

                    for(int n=0;n<filas;n++)
                    {
                      for(int m=0;m<columnas;m++)
                      {
                        numCadena[y]= String.valueOf(resultTable.getValueAt(n,m));
                        y++;
                      }
                    }
                    
                    String enc[]=new String[10];
                      for(int m=0;m<columnas;m++)
                     {
                       enc[m]=String.valueOf(resultTable.getColumnModel().getColumn(m).getHeaderValue());
                     }

                    PdfPTable tabla=new PdfPTable (columnas);
                    
                    for(int i=0;i<columnas;i++)
                    {
                      tabla.addCell(new Paragraph(enc[i],FontFactory.getFont("arial",8,BaseColor.BLACK)));

                    }
                    
                    int total=columnas*filas;


                     for(int i=0;i<total;i++)
                     {
                      tabla.addCell(new Paragraph(numCadena[i],FontFactory.getFont("arial",6,BaseColor.DARK_GRAY)));
                     }
                     
              
                     document.add(par);
                     document.add(new Paragraph("  "));
                     document.add(new Paragraph("  "));
                     document.add(tabla);
                     document.close();
                    
        } catch(Exception e){
            e.printStackTrace();
        }
            }   
    }//GEN-LAST:event_jToggleButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new cliente().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField apemTF;
    private javax.swing.JTextField apepTF;
    private javax.swing.JTextField correoTF;
    private javax.swing.JTextField direccionTF;
    private javax.swing.JTextField idTF;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JTable resultTable;
    private javax.swing.JTextField rfcTF;
    private javax.swing.JTextField telefonoTF;
    // End of variables declaration//GEN-END:variables
}
